package com.kgc.crowd.distributioncrowddatabaseprovider.service;

import com.kgc.crowd.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crowd.distributioncrowddatabaseprovider.mapper.MemberPOMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import javax.annotation.Resource;
@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {
   @Resource
    MemberPOMapper memberPOMapper;

    @Override
    public Integer getLoignAcctCount(String loginAcct) {
        // 1.创建MemberPOExample
        MemberPOExample example = new MemberPOExample();

        // 2.封装查询条件
        example.createCriteria().andLoginacctEqualTo(loginAcct);

        // 3.执行查询
        return memberPOMapper.countByExample(example);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    @Override
    public void saveMemberPO(MemberPO memberPO) {
        memberPOMapper.insert(memberPO);
    }

    @Override
    public MemberPO getMemberByLoginAcct(String loginAcct) {
        // 1.创建MemberPOExample对象
        MemberPOExample example = new MemberPOExample();

        // 2.封装查询条件
        example.createCriteria().andLoginacctEqualTo(loginAcct);

        // 3.执行QBC查询
        List<MemberPO> memberList = memberPOMapper.selectByExample(example);

        // 4.判断查询结果集合的有效性
        if(CrowdUtils.collectionEffectiveCheck(memberList)) {

            // 5.如果查询结果集合有效，则返回第一个元素
            return memberList.get(0);
        }

        // 6.如果集合无效则返回null
        return null;
    }
}